/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewCartridge.java
 * 模块说明：	
 * 修改历史：
 * 2006-6-29 - lxm - 创建。
 */
package com.hd123.rumba.action.cartridge;

import java.util.List;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.cartridge.CCartFunctionView;
import com.hd123.rumba.cartridge.CCartridge;
import com.hd123.rumba.cartridge.biz.BCartFunctionView;
import com.hd123.rumba.cartridge.biz.BCartridge;
import com.hd123.rumba.cartridge.biz.IBCartridgeMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.log.ActionLogger;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.request.JspLib;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.user.IPermissionMgr;

/**
 * 构件包 | Action | 基类
 * 
 * @author lxm
 * 
 */
public abstract class BaseViewCartridge extends ActionAbstractImpl {

  /* Templates */
  private static final String TEMPLATE_LIST = "cartridge/CartridgesList.jsp";
  private static final String TEMPLATE_VIEW = "cartridge/CartridgeView.jsp";
  private static final String TEMPLATE_FUNCVIEW_VIEW = "cartridge/FuncViewView.jsp";

  public String list() throws Exception {
    logger.info(ActionLogger.EVENT_LIST, null);
    List<BCartridge> carts = getCartMgr().getAllCartridges();
    dd.put("carts", carts);
    dd.setWorkingTemplate(TEMPLATE_LIST);
    return ActionConstants.APPLICATIONFRAME;
  }
  
  public String view() throws Exception {
    logger.info(ActionLogger.EVENT_VIEW, CCartridge.CAPTION_CLASS);
    
    String uuid = (String) dd.getFormValue("cart_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数cart_uuid。");
    String tabView = (String) dd.getFormValue("tabView");
    if (tabView == null)
      tabView = "tvFuncViews";
    
    BCartridge cart = getCartMgr().getCartridge(uuid);
    
    dd.put("tabView", tabView);
    return showDetail(cart, JspLib.VALUE_FORMSTATE_VIEW);
  }
  
  public String viewFuncView() throws Exception {
    logger.info(ActionLogger.EVENT_VIEW, CCartFunctionView.CAPTION_CLASS);
    
    String uuid = (String) dd.getFormValue("cfv_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数cfv_uuid。");
    String tabView = (String) dd.getFormValue("tabView");
    if (tabView == null)
      tabView = "tvReferences";
    
    BCartFunctionView funcView = getCartMgr().getFunctionView(uuid);
    
    dd.put("tabView", tabView);
    return showFuncViewDetail(funcView, JspLib.VALUE_FORMSTATE_VIEW);
  }

  /**
   * 根据功能等信息取得权限ID。
   * 
   * @param functionName
   *          功能名，取值为类似“FUNCTION_*”的常量。
   * @return 返回权限ID，若没有权限对应则返回null。
   * @throws BusinessException
   * @throws HibernateException
   */
  public abstract String getPermission(String functionName)
      throws BusinessException, HibernateException;

  /**
   * 根据当前指定访问的域和是否是回收站，选择相应的权限放到dd中，以便页面使用。<br>
   * 各权限对应dd中的key见下表：<br> - "permissionCartridgeView": 构件包查看权<br> -
   * "permissionFuncViewView": 功能视图查看权。
   * 
   * @throws BusinessException
   */
  protected abstract void putFormPermissions() throws BusinessException;

  private IBCartridgeMgr cartMgr = null;

  protected IBCartridgeMgr getCartMgr() throws BusinessException {
    if (cartMgr == null)
      cartMgr = (IBCartridgeMgr) NamingMgr.getInstance().lookupMgr(IBCartridgeMgr.class);
    return cartMgr;
  }
  
  private String showDetail(BCartridge cart, String formState) throws Exception {
    dd.put("cart", cart);

    IPermissionMgr permMgr = (IPermissionMgr) NamingMgr.getInstance().lookupMgr(
        IPermissionMgr.class);
    dd.put("am", permMgr.createFieldsAccessMode(getCurrentUser(), CCartridge.class));
    this.putFormPermissions();
    dd.putFormValue(JspLib.ID_FORMSTATE, formState);
    dd.setWorkingTemplate(TEMPLATE_VIEW);
    return ActionConstants.APPLICATIONFRAME;
  }
  
  private String showFuncViewDetail(BCartFunctionView funcView, String formState) throws Exception {
    dd.put("funcView", funcView);

    IPermissionMgr permMgr = (IPermissionMgr) NamingMgr.getInstance().lookupMgr(
        IPermissionMgr.class);
    dd.put("am", permMgr.createFieldsAccessMode(getCurrentUser(), CCartFunctionView.class));
    this.putFormPermissions();
    dd.putFormValue(JspLib.ID_FORMSTATE, formState);
    dd.setWorkingTemplate(TEMPLATE_FUNCVIEW_VIEW);
    return ActionConstants.APPLICATIONFRAME;
  }

}
